// pages/product/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CDNPATH: app.globalData.CDNPATH,
    code: '',
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0
    },
    typeList: [{name: '约访', code: 'visit'}, {name: '寒暄', code: 'greeting'}, {name: '计划书', code: 'proposal'}, {name: '异议', code: 'objection'}, {name: '保险知识', code: 'insurance'}],
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.list.length <= 0) {
      this.getProductList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toView (e) {
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: `/myself/pages/score-detail/scoreDetail?practiceId=${e.currentTarget.id}`
    })
  },
  videoPlay: function (e) {
    console.log(e)
    const productVideoResourceId = e.currentTarget.dataset.videoid
    if (!productVideoResourceId) return
    var _index = e.currentTarget.id
    this.setData({
      _index: _index,
      scrollIndex: e.currentTarget.dataset.index
    })
    //停止正在播放的视频
    var videoContextPrev = wx.createVideoContext(this.data._index)
    videoContextPrev.stop();
    setTimeout(function(){
      //将点击视频进行播放
      var videoContext = wx.createVideoContext(_index)
      videoContext.requestFullScreen()
      videoContext.play();
    },500)
   
  },

  screenChange(e){
    let fullScreen = e.detail.fullScreen //值true为进入全屏，false为退出全屏
    if (!fullScreen ){ //退出全屏
      this.setData({
        controls: false,
        _index: -1,
        toView: 'item'+this.data.scrollIndex
      })
    }else{ //进入全屏
     this.setData({
        controls: true
      })
    }
 },
  tapType: function (e) {
    const { code } = e.currentTarget.dataset
    this.setData({
      code,
      list: [],
      page: {
        currentPage: 1,
        pageSize: 10
      }
    })
    this.getProductList()
  },
  getProductList: function (e) {
    const { code, page, list } = this.data
    const { currentPage, pageSize } = page
    app.api.post({
      url: 'query_practice_record_by_page',
      data: { currentPage, pageSize, type: code }
    }).then(res => {
      list.push(...res.list)
      this.setData({
        list,
        'page.total': Number(res.total)
      })
    })
  },
  loadMore: function () {
    const { currentPage, total } = this.data.page
    if (total > this.data.list.length) {
      this.setData({
        'page.currentPage': currentPage + 1
      })
      this.getProductList()
    }
  },
})